<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>公告管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<!-- 查询条件开始-->

<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form" >
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">公告标题</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" id="title" class="layui-input searchVal" placeholder="请输入公告标题" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">发布人</label>
                    <div class="layui-input-inline">
                        <input type="text" name="opername" id="opername" class="layui-input searchVal" placeholder="请输入发布人" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="startTime" id="startTime" readonly class="layui-input searchVal" placeholder="开始时间" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">结束时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="endTime" id="endTime" readonly class="layui-input searchVal" placeholder="结束时间" />
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"><span class="layui-icon layui-icon-search"></span>查询</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="noticeTable" lay-filter="noticeTable"></table>
    <!-- 工具栏 -->
    <div id="noticeToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加公告</button>
        <button type="button" lay-event="batchDelete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>批量删除</button>
    </div>

    <div id="noticeRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
        <button type="button" lay-event="show" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-about"></span>查看</button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->

<div  style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form class="layui-form" style="width:80%;" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">公告标题</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" class="layui-input" name="title"  lay-verify="required" placeholder="请输入标题">
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">公告内容</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入公告内容" name="content" id="content" class="layui-textarea userDesc"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>

<!-- 添加和修改的弹出层结束 -->

<!-- 查看弹出层的开始 -->
<div style="display: none;padding: 5px" id="showNoticeDiv">
    <h2 id="show_title" align="center"></h2>
    <div style="text-align: right;margin: 10px">
        <span>发布人:<span id="show_opername"></span></span>
        <span style="padding-left: 20px">发布时间:<span id="show_createtime"></span></span>
    </div>
    <hr>
    <div id="show_content" style="margin: 10px;padding: 10px;line-height: 25px;"></div>

</div>
<!-- 查看弹出层的结束-->



<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery','form','table','layer','laydate','layedit'],function () {
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var laydate=layui.laydate;
        var layedit=layui.layedit;
        //初始化时间选择器
        laydate.render({
            elem:'#startTime',//绑定渲染的元素
            type:'datetime'//控件选择类型
        });
        laydate.render({
            elem:'#endTime',//绑定渲染的元素
            type:'datetime'//控件选择类型
        });

        //表格渲染
        var tableIns=table.render({
            elem: '#noticeTable'
            ,url:'/sys/notice/list'
            ,toolbar: '#noticeToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,page: true
            ,title: '账单数据表'
            ,cols: [  [
                {type:'checkbox'}
                ,{field:'id',title:'ID',align:'center'}
                ,{field:'title',title:'账单标题',align:'center'}
                ,{field:'createtime',title:'创建时间',align:'center'}
                ,{field:'modifytime',title:'修改时间',align:'center'}
                ,{field:'opername',title:'发布人',align:'center'}
                ,{title:'操作',toolbar:'#noticeRowBar'}
            ]  ],
            done:function (res,curr,count) {
                //判断当前页面是否大于1并且当前页的数据为0
                if (curr>1&&res.data.length==0){
                    var pageValue=curr-1
                    //刷新数据表格
                    tableIns.reload({
                        page:{curr:pageValue}
                    })
                }
            }
        });

        //监听表单提交事件
        form.on("submit(doSearch)",function (data) {
            tableIns.reload({
                url:'/sys/notice/list',
                where:data.field,
                page:{
                    curr:1
                }

            });
            return false;
        });

        //监听头部工具栏事件
        table.on('toolbar(noticeTable)',function (obj) {
            switch (obj.event) {
                case 'add':
                    //打开头部工具栏添加公告的窗口
                    openAddNotic();
                    break;
                case 'batchDelete':
                    deleteNoticeList();//批量删除
                    break;
            }
        })


        //监听行工具栏事件
        table.on('tool(noticeTable)',function (obj) {
            switch (obj.event) {
                case 'update':
                    //打开修改公告
                    openUpdateNotic(obj.data);
                    break;
                case 'delete':
                    deleteNotic(obj.data);//id删除
                    break;
                case 'show':
                     showLook(obj.data); //查看
                    break;
            }
        })






        var mainIndex;//打开窗口的索引
        var url;//提交请求的地址
        var editIndex;//编辑器

        /**
         * 打开添加公告窗口
         */
        function openAddNotic(){
                mainIndex=layer.open({
                    type:1,
                    content:$("#addOrUpdateDiv"),
                    area:["800px","500px"],
                    title:"发布公告",
                    success:function () {
                        //每次打开窗口，清空表单数据
                        $("#dataFrm")[0].reset();
                        //富文本编辑器内容清空
                        layedit.setContent(editIndex,"");
                        //提交数据
                        url="/sys/notice/addNotice";

                    }
                });
            //初始化富文本编辑器,如果不初始化编辑器那么是只有显示文本框，没有加粗或者是表情或者是插图样式显示
            editIndex=layedit.build('content');//建立编辑器,参数是放文本域的id
        }

        /**
         * 批量删除
         */
        function deleteNoticeList(){
            //获取当前选中的行
            var checkStatus=table.checkStatus('noticeTable');
            //判断是否有选中行
            var length=checkStatus.data.length;
            if (length>0){
                layer.confirm('确认要删除这些数据吗?',{icon:3,"title":"提示"},function (index) {
                    //获取选中行数据
                    var data=checkStatus.data;
                    var idArr=[];   //创建一个数组
                    for (var i=0;i<length;i++){
                        idArr.push(data[i].id);

                    }
                    //id数组
                    var ids=idArr.join(",");
                    $.post("/sys/notice/deleteNoticeList", {"ids":ids}, function (result) {
                        if (result.success){
                            tableIns.reload();//成功后表格重新加载
                        }
                        //提示
                        layer.msg(result.message);
                    }, "json");


                })
            }else {
                layer.alert('请选中要删除的行',{icon:0})
            }

        }

        /**
         * 修改
         * @param data
         */
        function openUpdateNotic(data){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:["800px","500px"],
                title:"更新公告",
                success:function () {
                    //富文本编辑器内容清空
                    layedit.setContent(editIndex,data.content);
                    //数据回显显示
                    form.val("dataFrm",data);
                    //提交数据
                    url="/sys/notice/updateNotice";
                }
            });
            //初始化富文本编辑器,如果不初始化编辑器那么是只有显示文本框，没有加粗或者是表情或者是插图样式显示
            editIndex=layedit.build('content');//建立编辑器,参数是放文本域的id
        }

        /**
         * id删除
         */
        function deleteNotic(data){
            layer.confirm('是否要删除该数据？', {"icon":3,"title":"提示"},function (index) {
                $.post("/sys/notice/deleteById", {"id":data.id}, function (result) {
                    console.log(result);
                    if (result.success){
                        tableIns.reload();//表格重新加载
                    }
                    layer.msg(result.message);
                    layer.close(index);
                }, "json");
                return false;
            });
        }

        /**
         * 查看
         */
        function showLook(data){
                layer.open({
                    type:1,//弹出层类型
                    content:$("#showNoticeDiv"),
                    area:["800px","500px"],
                    title:"查看公告",
                    success:function () {
                        $("#show_title").html(data.title);
                        $("#show_opername").html(data.opername);
                        $("#show_createtime").html(data.createtime);
                        $("#show_content").html(data.content);
                    }

                })
        }


        /**
         * 监听表单提交事件
         */
        form.on("submit(doSubmit)",function (data) {
            //将layedit内容同步到textaareaw文本域中，不然添加后就会发现context内容是没有添加显示的
            layedit.sync(editIndex);

            $.post(url,$("#dataFrm").serialize(),function (result) {
                console.log(result);
                if(result.success){
                    //提示信息
                    layer.msg(result.message,{icon:1});
                    //刷新数据表格
                    tableIns.reload();//刷新数据表格
                    layer.close(mainIndex);//窗口关闭
                }else {
                    layer.msg(result.message,{icon:2});
                }


            },"json");
            return false;
        });
    });
</script>
</body>
</html>